package com.axiyue.trip.response;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.axiyue.trip.request.TripRequest;

public class TripUtil {

	public static enum REQUEST_TYPE {
		SINGLE_TRIP_FLIGHT_PRICE
		,ROUND_TRIP_FLIGHT_PRICE
		,FLIGHT_STOPS
		,SPEC_FLIGTH_PRICE
		,SUBMIT_ORDER
		,CANCEL_ORDER
		,CHECKCABIN
		,ASKORDERTICKET
		,VALID_PRICE
		,PAY_NOTICE
		,GET_ORDERID
		,GET_STATUS_BY_ORDERID
		,GET_ORDER_DETAIL
		,QUERY_WEB_FLIGHTS
	};
	

	public static final SimpleDateFormat DF_MM_DD = new SimpleDateFormat(
			"MM月dd日");
	public static final SimpleDateFormat DF_YYYY_MM_DD = new SimpleDateFormat(
			"yyyy-MM-dd");

	public static final String TICKET_INFO = "response.xml";
	public static final String STOPS_STATION = "stopsStation.xml";
	public static final String SPEC_TICKET = "specPrice.xml";
	public static final Integer DEFAULT_SPEC_PRICE_DAYS = 7;

	public static final String ERROR_10000 = "[1E系统]无法识别的主机错误信息！";
	public static final String ERROR_10099 = "[1E系统]输入格式错误！";
	public static final String ERROR_10101 = "[1E系统]没有航线";
	public static final String ERROR_10102 = "日期错误";
	public static final String ERROR_10103 = "航空公司代码不正确！";
	public static final String ERROR_10104 = "给出的航班号不正确！";
	public static final String ERROR_10105 = "给出的城市对不正确！";
	public static final String ERROR_10106 = "[1E系统]AV:fltno/date 格式查询的航班号在查询的日期不执行！";
	public static final String ERROR_50000 = "未知错误";
	public static final String ERROR_50001 = "请求数据错误";
	public static final String ERROR_50002 = "XML请求数据信息不完整";
	public static final String ERROR_50003 = "此方法调用未实现此方法调用未实现";
	public static final String ERROR_50004 = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
	public static final String ERROR_50101 = "不是合法的用户注册名！";
	public static final String ERROR_50102 = "输入的密码不合法";
	public static final String ERROR_50103 = "用户名或密码错";
	public static final String ERROR_50104 = "用户密码错";
	public static final String ERROR_50105 = "您的登录帐号已经被注销";
	public static final String ERROR_50106 = "您的登录帐号已过期";
	public static final String ERROR_50107 = "该客户已经被注销";
	public static final String ERROR_50108 = "登陆失败，请重新登陆！（错误原因：登陆通行证生成错误）";
	public static final String ERROR_50109 = "无效的验证信息(passportID)";
	public static final String ERROR_50110 = "断开登录失败，请重试或和管理员联系";
	public static final String ERROR_50111 = "该帐号没有登录";
	public static final String ERROR_50112 = "用户密码错";
	public static final String ERROR_50113 = "该登陆员不存在";
	public static final String ERROR_50114 = "由于您长时间没有进行操作，登录已过期，请重新登录系统。";
	public static final String ERROR_50115 = "您的帐号在另一地点登录，您被迫下线，如果不是您本人的操作，那么您的密码  可能已经泄露，建议您重新登录系统后修改密码！";
	public static final String ERROR_50200 = "客票格式不正确！";
	public static final String ERROR_50201 = "指定日期没有满足条件的航班";
	public static final String ERROR_50500 = "预订请求数据不完整";
	public static final String ERROR_50501 = "客票格式不正确";
	public static final String ERROR_50502 = "婴儿必需由成人陪伴,且一成人只能携带一名婴儿";
	public static final String ERROR_50503 = "创建的PNR 不是HK 状态!";
	public static final String ERROR_50504 = "传入的回调通知参数有误，请传入回调通知方式。如果要求回调通知 回调地址、回调方式 为须填，反之都为空";
	public static final String ERROR_50505 = "订单已经超过了系统指定的出票时限";
	public static final String ERROR_50506 = "结算帐单生成失败,请重新提交或与管理员联系";
	public static final String ERROR_50507 = "结算帐单生成失败,请重新提交或与管理员联系";
	public static final String ERROR_50508 = "文件字节(大小)于传入的文件字节数不匹配,为确保数据完整性,请核定数据大小";
	public static final String ERROR_50509 = "结算帐单与数据库帐单数据量匹配,操作失败";
	public static final String ERROR_50510 = "结算日期格式有误!正确格式:YYYYMMDDHHmmss或 YYYYMMDD";
	public static final String ERROR_50511 = "无结算帐单请检查结算文件是否正确!";
	public static final String ERROR_60000 = "没有查询到符合订单号";
	public static final String ERROR_60001 = "未定义错误";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
//		TripRequest request = getRequestInstance(REQUEST_TYPE.SINGLE_TRIP_FLIGHT_PRICE);
//		System.out.println(gernateXml(request));
//		;
		
		System.out.println(recalculator("2012-10-11", 10));
	}

	public static String gernateXml(Object obj) throws Exception {
		if (obj != null) {
			try {
				JAXBContext context = JAXBContext.newInstance(obj.getClass());
				Marshaller m = context.createMarshaller();
				m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
				Writer writer = new StringWriter();
				m.marshal(obj, writer);
				try {
					String xml = writer.toString();
					writer.flush();
					return xml;
				} catch (IOException e) {
					return "";
				} finally {
					if (writer != null)
						writer.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("失败!");
			}
		} else {
			return null;
		}
	}

	public static InputStream convert(String str) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return is;
	}

	public static TripResponse getStopsStation(String str) {
		JAXBContext context;
		TripResponse response = null;
		try {
			context = JAXBContext.newInstance(TripResponse.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			URL xmlFileUrl = TripUtil.class.getClassLoader().getResource(
					STOPS_STATION);
			response = (TripResponse) unmarshaller.unmarshal(xmlFileUrl);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static TripResponse getTicketInfo() {
		JAXBContext context;
		TripResponse response = null;
		try {
			context = JAXBContext.newInstance(TripResponse.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			URL xmlFileUrl = TripUtil.class.getClassLoader().getResource(
					TICKET_INFO);
			response = (TripResponse) unmarshaller.unmarshal(xmlFileUrl);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static TripResponse getTicketInfoStr(String str) {
		JAXBContext context;
		TripResponse response = null;
		try {
			context = JAXBContext.newInstance(TripResponse.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			response = (TripResponse) unmarshaller.unmarshal(convert(str));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static TripResponse getStopsStation() {
		JAXBContext context;
		TripResponse response = null;
		try {
			context = JAXBContext.newInstance(TripResponse.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			URL xmlFileUrl = TripUtil.class.getClassLoader().getResource(
					STOPS_STATION);
			response = (TripResponse) unmarshaller.unmarshal(xmlFileUrl);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static TripResponse getSpecTicket() {
		JAXBContext context;
		TripResponse response = null;
		try {
			context = JAXBContext.newInstance(TripResponse.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			URL xmlFileUrl = TripUtil.class.getClassLoader().getResource(
					SPEC_TICKET);
			response = (TripResponse) unmarshaller.unmarshal(xmlFileUrl);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static TripRequest getRequestInstance(REQUEST_TYPE type) {
		TripRequest request = new TripRequest();
		com.axiyue.trip.request.Header header = new com.axiyue.trip.request.Header();
		com.axiyue.trip.request.Body body = new com.axiyue.trip.request.Body();

		request.setHeader(header);
		request.setBody(body);
//bycxysc01
		header.setAccountID("bycxysc01");
//		1234
		header.setDigitalSign("F74F684FEC21E74C");
		header.setGzip("0");

		switch (type) {
		case SINGLE_TRIP_FLIGHT_PRICE:
			header.setServiceName("getFlightPrice");
			break;
		case ROUND_TRIP_FLIGHT_PRICE:
			// getRoundTripFlightPrice
			header.setServiceName("getRoundTripFlightPrice");
			break;
		case FLIGHT_STOPS:
			// getFlightStop
			header.setServiceName("getFlightStop");
			break;
		case SPEC_FLIGTH_PRICE:
			// getSpecFlightPrice
			header.setServiceName("getSpecFlightPrice");
			break;
		case SUBMIT_ORDER:
			header.setServiceName("orderFlight");
			break;
		case CANCEL_ORDER:
			header.setServiceName("submitOrder");
			break;
		case CHECKCABIN:
			header.setServiceName("checkFlightCabin");
			break;
		case ASKORDERTICKET:
//			askOrderTicket

			header.setServiceName("askOrderTicket");
			break;
		case GET_ORDERID:
			header.setServiceName("submitOrder");
			break;
		case GET_STATUS_BY_ORDERID:
			header.setServiceName("submitOrder");
			break;
		case GET_ORDER_DETAIL:
			header.setServiceName("submitOrder");
			break;
		case PAY_NOTICE:
			header.setServiceName("submitOrder");
			break;
		case VALID_PRICE:
			header.setServiceName("submitOrder");
			break;
		case QUERY_WEB_FLIGHTS:
			header.setServiceName("submitOrder");
			break;
		default:
			break;
		}
		return request;

	}
	
	public static String recalculator(String from,int days) throws Exception
	{
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sf.parse(from);
		Date ret = new Date(date.getTime()-(days/2)*3600000*24);
		return sf.format(ret);
	}
	
	
	public static void main1(String[] args) throws Exception {
		System.out.println(recalculator("2012-10-11", 8));
	}
	

}
